package com.example.blockchain.timer;

import com.example.blockchain.entity.block.*;
import com.example.blockchain.service.impl.DataDealServiceImpl;
import com.example.blockchain.utils.InitUtils;
import io.ep2p.kademlia.netty.common.NettyConnectionInfo;
import io.ep2p.kademlia.node.external.ExternalNode;
import io.ep2p.kademlia.table.Bucket;
import io.ep2p.kademlia.table.RoutingTable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;

@Component
public class UpdateTimer {

    public static BigInteger currentBlockIndex = new BigInteger("0");//当前缓存里面的块
    public static BigInteger currentMaxBlockIndex = new BigInteger("0");//当前最大的区块号

    @Resource
    private MapFriends map;
    @Resource
    private Mining mining;
    @Resource
    private Node node;
    @Resource
    private DataDealServiceImpl update;
    @Value("${node.ip}")
    private String localIP;


    @Scheduled(cron = "* * * * * ?")
    public void deal1() throws InterruptedException {

        Thread.sleep(20000);

        Dictionary max = InitUtils.intiMainChainDic();
        Dictionary now = InitUtils.intiDic();
        currentBlockIndex = new BigInteger(now.getValue());
        currentMaxBlockIndex = new BigInteger(max.getValue());
        currentBlockIndex = currentBlockIndex.add(BigInteger.ONE);

        while (true)
            try {
                if (node.getNode() != null) {
                    //InetAddress localHost = InetAddress.getLocalHost();
                    //String localIP = localHost.getHostAddress();
                    RoutingTable<BigInteger, NettyConnectionInfo, Bucket<BigInteger, NettyConnectionInfo>> routingTable = node.getNode().getRoutingTable();
                    for (Bucket<BigInteger, NettyConnectionInfo> bucket : routingTable.getBuckets()) {
                        List<BigInteger> nodeIds = bucket.getNodeIds();

                        if (nodeIds.size() != 0) {
                            for (BigInteger nodeId : nodeIds) {
                                ExternalNode<BigInteger, NettyConnectionInfo> nodeInfo = bucket.getNode(nodeId);
                                System.out.println(nodeInfo);
                                NettyConnectionInfo connectionInfo = nodeInfo.getConnectionInfo();
                                System.out.println(connectionInfo);
                                Friends f = map.getFs().get(connectionInfo.getHost());
                                if (f == null) {
                                    String ip = connectionInfo.getHost();
                                    //if (!ip.equals(localIP)) {
                                        Friends friends = new Friends();
                                        friends.setIp(connectionInfo.getHost());
                                        friends.setFriendliness(100);
                                        map.getFs().put(ip, friends);
                                    //}

                                }
                            }
                        }
                    }
                    System.out.println("成功");

                    System.out.println("blockIndex------------:" + currentBlockIndex);
                    update.updateBlock(currentBlockIndex.toString(), mining, map);
                    currentBlockIndex = currentBlockIndex.add(BigInteger.ONE);

                }

            } catch (Exception e) {
                Thread.sleep(1000);
                System.out.println("当前区块无需更新");

            }finally {
                mining.isWork=true;
                mining.updateComplete=true;
            }
    }

}
